package com.ymatou.query.seller.trading.api.parameter;

import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 对应存储过程：sp_trd_GetSellerOrdersListSummaryV2
 * 
 * @author songyefeng
 *
 */
public class Sp_GetSellerOrdersListSummaryV2 {
	private YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();

	private int sellerId;
	private Integer orderSellerAcceptedStatus;
	private List<Integer> orderStatus;
	private Boolean paidInFull;
	private String timeFrom;
	private String timeTo;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;
	private int filter;
	private Object filterValue;

	public int getSellerId() {
		return sellerId;
	}

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public Integer getOrderSellerAcceptedStatus() {
		return orderSellerAcceptedStatus;
	}

	public void setOrderSellerAcceptedStatus(Integer orderSellerAcceptedStatus) {
		this.orderSellerAcceptedStatus = orderSellerAcceptedStatus;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Boolean getPaidInFull() {
		return paidInFull;
	}

	public void setPaidInFull(Boolean paidInFull) {
		this.paidInFull = paidInFull;
	}

	public String getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public String getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	public Integer getFilter() {
		return filter;
	}

	public void setFilter(Integer filter) {
		this.filter = filter;
	}

	public Object getFilterValue() {
		return filterValue;
	}

	public void setFilterValue(Object filterValue) {
		this.filterValue = filterValue;
	}

	@SuppressWarnings("rawtypes")
	public List<Map> execute() {
		String queryField = "count(o.iOrderId) OrdersCount,"
				// + "sum("
				// + "IIF(o.bShangouOrder = 1"
				// +
				// ", o.fOrderPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - isnull(o.fYmtCouponAmount, 0) - isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0)"
				// +
				// "+ isnull( p.fAmount, IIF(o.bPaidInFull = 1, 0, (o.fTotalPrice - o.fOrderPrice)) )"
				// + "- isnull(p.fUseGiftAmount, 0)"
				// +
				// ", o.fTotalPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - isnull(o.fYmtCouponAmount, 0) - isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0)"
				// + ")) TotalPrice";
				+ "sum(o.fOrderPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - isnull(o.fYmtCouponAmount, 0) -"
				+ "isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0) +"
				+ "isnull( p.fAmount, o.fTotalPrice - o.fOrderPrice))  ExpectedTotalPrice, "
				+ "sum(o.fOrderPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - "
				+ "isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0) +"
				+ "isnull( p.fAmount, o.fTotalPrice - o.fOrderPrice))  ExpectedTotalPriceYMTCoupon, "
				/**
				 * 2016-11-07
				 * 增加条件 iff(s.fPaidAmountOfCash<0.01
				 */
				+ "sum(iif(s.fPaidAmountOfCash<0.01,0,o.fTotalPrice + o.fFreight)) ExpectedSales";
		String joinCondition = " with (nolock,index = IX_Ymt_Orders)  "
				+ " inner loop join Ymt_OrderState s with (nolock,index = PK_Ymt_OrderState)  on o.iOrderId = s.iOrderId"
				+ " outer apply (select top 1 fAmount,fUseGiftAmount from Ymt_OrderPostPay with (nolock,index = IX_Ymt_OrderPostPay) where iOrderId = o.iOrderId and (iAction = 0 or iAction = 1) order by iAction desc) p";
		StringBuffer queryCondition = new StringBuffer();
		queryCondition.append("o.iBuyerId = " + sellerId);
		boolean addOr = false;
		if (null != paidInFull || false == DataUtil.isNullOrEmpty(orderStatus)) {
			queryCondition.append(" and (");
			if (null != paidInFull) {
				queryCondition.append(" (o.iTradingStatus = "
						+ orderSellerAcceptedStatus + " and o.bPaidInFull = "
						+ DataUtil.bool2Int(paidInFull) + ")");
				addOr = true;
			}
			if (false == DataUtil.isNullOrEmpty(orderStatus)) {
				if (addOr) {
					queryCondition.append(" or");
				}
				queryCondition.append(" o.iTradingStatus in "
						+ DataUtil.toInOfSql(orderStatus));
			}
			queryCondition.append(" )");
		}
		if (timeFrom != null) {
			queryCondition.append(" and o.dAddTime >= '" + timeFrom + "'");
		}
		if (timeTo != null) {
			queryCondition.append(" and o.dAddTime < '" + timeTo + "'");
		}
		if (filter == 1) {
			queryCondition.append(" and o.iOrderId like '%" + filterValue
					+ "%'");
		}
		if (filter == 2) {
			queryCondition.append(" and o.sBuyerNickName like '%" + filterValue
					+ "%'");
		}
		if (filter == 3) {
			queryCondition.append(" and o.sReceivePerson like '%" + filterValue
					+ "%'");
		}
		if (filter == 4) {
			queryCondition
					.append(" and exists(select top 1 1 from Ymt_OrderSummary(nolock) where iOrderId = o.iOrderId and sSummary like '%"
							+ filterValue + "%')");
		}
		return ymtOrdersWapper.selectOrders2(queryField, joinCondition,
				queryCondition.toString());
	}
}
